﻿//using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;

//Class dùng để handle ComboBox
//Hỗ trợ auto complete
public class ComboBoxHandle : NativeWindow
{

    private ComboBox withEventsField_objComboBox;
    private ComboBox objComboBox
    {
        get { return withEventsField_objComboBox; }
        set
        {
            if (withEventsField_objComboBox != null)
            {
                withEventsField_objComboBox.Enter -= objComboBox_Enter;
                withEventsField_objComboBox.Click -= objComboBox_Click;
                withEventsField_objComboBox.Validated -= objComboBox_Validated;
                withEventsField_objComboBox.KeyPress -= objComboBox_KeyPress;
                withEventsField_objComboBox.SelectedIndexChanged -= objComboBox_SelectedIndexChanged;
                withEventsField_objComboBox.TextChanged -= objComboBox_TextChanged;
                withEventsField_objComboBox.TextUpdate -= objComboBox_TextUpdate;
                withEventsField_objComboBox.Validating -= objComboBox_Validating;
            }
            withEventsField_objComboBox = value;
            if (withEventsField_objComboBox != null)
            {
                withEventsField_objComboBox.Enter += objComboBox_Enter;
                withEventsField_objComboBox.Click += objComboBox_Click;
                withEventsField_objComboBox.Validated += objComboBox_Validated;
                withEventsField_objComboBox.KeyPress += objComboBox_KeyPress;
                withEventsField_objComboBox.SelectedIndexChanged += objComboBox_SelectedIndexChanged;
                withEventsField_objComboBox.TextChanged += objComboBox_TextChanged;
                withEventsField_objComboBox.TextUpdate += objComboBox_TextUpdate;
                withEventsField_objComboBox.Validating += objComboBox_Validating;
            }
        }

    }
    private ComboBoxHandle()
    {
    }

    public ComboBoxHandle(ComboBox tb)
    {
        this.objComboBox = tb;
        objComboBox.DropDownStyle = ComboBoxStyle.DropDown;
    }

    private void objComboBox_Enter(object sender, System.EventArgs e)
    {
        try
        {
            //If Not objComboBox.DroppedDown Then objComboBox.DroppedDown = True
        }
        catch (Exception exp)
        {
            FormGlobals.Message_Error(exp);
        }
    }

    private void objComboBox_Click(object sender, System.EventArgs e)
    {
        try
        {
            //If Not objComboBox.DroppedDown Then objComboBox.DroppedDown = True
        }
        catch (Exception exp)
        {
            FormGlobals.Message_Error(exp);
        }
    }

    private void objComboBox_Validated(object sender, System.EventArgs e)
    {
        try
        {
            if (!objComboBox.Enabled)
                return;

            int iIndex = objComboBox.FindString(objComboBox.Text);
            if (iIndex >= 0)
            {
                //objComboBox.SelectedIndex = -1
                objComboBox.SelectedIndex = iIndex;
            }
            else
            {
                if (objComboBox.Items.Count > 0)
                {
                    objComboBox.SelectedIndex = 0;
                }
                else
                {
                    objComboBox.Text = "";
                }
            }
            //If objComboBox.DroppedDown Then objComboBox.DroppedDown = False           
        }
        catch (Exception exp)
        {
            FormGlobals.Message_Error(exp);
        }
    }

    //Private Sub objComboBox_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles objComboBox.PreviewKeyDown
    //    If e.KeyCode = Keys.Enter Then SendKeys.SendWait("{TAB}")
    //End Sub

    private void objComboBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if (e.KeyChar.ToString() == Globals.vbCr)
        {
            e.Handled = true;
            SendKeys.Send("{TAB}");
        }
    }

    private void objComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        if (objComboBox.Parent.Controls.ContainsKey(objComboBox.Name + "_View"))
        {
            TextBox ctlView = objComboBox.Parent.Controls[objComboBox.Name + "_View"] as TextBox;
            ctlView.Text = objComboBox.Text;
        }
    }


    private void objComboBox_TextChanged(object sender, System.EventArgs e)
    {
    }

    private void objComboBox_TextUpdate(object sender, System.EventArgs e)
    {
        //If objComboBox.ContainsFocus Then
        //    Dim dtView As DataView
        //    If TypeOf objComboBox.DataSource Is DataTable Then
        //        dtView = DirectCast(objComboBox.DataSource, DataTable).DefaultView
        //    ElseIf TypeOf objComboBox.DataSource Is DataView Then
        //        dtView = DirectCast(objComboBox.DataSource, DataView)
        //    Else
        //        Return
        //    End If
        //    If dtView.Table.Columns.Contains("Sort_Field") Then dtView.RowFilter = "Sort_Field LIKE '" + objComboBox.Text + "%'"
        //End If
    }

    private void objComboBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        DataView dtView = default(DataView);
        if (objComboBox.DataSource is DataTable)
        {
            dtView = ((DataTable)objComboBox.DataSource).DefaultView;
        }
        else if (objComboBox.DataSource is DataView)
        {
            dtView = (DataView)objComboBox.DataSource;
        }
        else
        {
            return;
        }
        string sOldText = objComboBox.Text;
        dtView.RowFilter = "";
        objComboBox.Text = sOldText;
    }

    //Private Sub objComboBox_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles objComboBox.VisibleChanged
    //    If Not objComboBox.Parent Is Nothing AndAlso objComboBox.Parent.Controls.ContainsKey(objComboBox.Name + "_Flat") Then
    //        objComboBox.Parent.Controls(objComboBox.Name + "_Flat").Visible = objComboBox.Visible
    //    End If
    //End Sub
}
